package com.shanzhu.parking.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shanzhu.parking.common.R;
import com.shanzhu.parking.entity.Violation;
import com.shanzhu.parking.entity.query.ViolationQuery;
import com.shanzhu.parking.service.ViolationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import cn.hutool.core.util.StrUtil;

import java.time.LocalDateTime;

/**
 * <p>
 * 车辆违规记录表 前端控制器
 * </p>
 *
 * @author 只茂瑞
 * @since 2025-04-13
 */
@Slf4j
@RestController
@RequestMapping("/violation")
@RequiredArgsConstructor
@Api(tags = "违规记录管理")
public class ViolationController {

    private final ViolationService violationService;

    /**
     * 分页查询违规记录列表
     *
     * @param violationQuery 查询条件
     * @return 分页结果
     */
    @PostMapping("/list")
    @ApiOperation("分页查询违规记录列表")
    public R<IPage<Violation>> list(@RequestBody ViolationQuery violationQuery) {
        log.info("分页查询违规记录列表: {}", violationQuery);
        
        // 创建分页对象
        Page<Violation> page = new Page<>(violationQuery.getPagenum(), violationQuery.getPageSize());
        
        // 创建查询条件
        LambdaQueryWrapper<Violation> queryWrapper = new LambdaQueryWrapper<>();
        
        // 车牌号模糊查询
        if (StrUtil.isNotBlank(violationQuery.getPlateNumber())) {
            queryWrapper.like(Violation::getPlateNumber, violationQuery.getPlateNumber());
        }
        
        // 违规类型查询
        if (StrUtil.isNotBlank(violationQuery.getViolationType())) {
            queryWrapper.eq(Violation::getViolationType, violationQuery.getViolationType());
        }
        
        // 状态查询
        if (StrUtil.isNotBlank(violationQuery.getStatus())) {
            queryWrapper.eq(Violation::getStatus, violationQuery.getStatus());
        }
        
        // 按时间降序排序
        queryWrapper.orderByDesc(Violation::getViolationTime);
        
        // 执行查询
        IPage<Violation> violationPage = violationService.page(page, queryWrapper);
        return R.success(violationPage);
    }

    /**
     * 添加违规记录
     *
     * @param violation 违规信息
     * @return 添加结果
     */
    @PostMapping("/add")
    @ApiOperation("添加违规记录")
    public R<Boolean> add(@RequestBody Violation violation) {
        log.info("添加违规记录: {}", violation);
        
        // 设置默认状态为未处理
        violation.setStatus("0");
        
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        violation.setCreateTime(now);
        violation.setUpdateTime(now);
        
        // 保存违规记录
        boolean result = violationService.save(violation);
        return R.success(result);
    }

    /**
     * 处理违规记录
     *
     * @param violation 处理信息
     * @return 处理结果
     */
    @PostMapping("/handle")
    @ApiOperation("处理违规记录")
    public R<Boolean> handle(@RequestBody Violation violation) {
        log.info("处理违规记录: {}", violation);
        
        // 如果缺少ID则返回失败
        if (violation.getId() == null) {
            return R.error(false);
        }
        
        // 设置处理时间
        violation.setHandleTime(LocalDateTime.now());
        
        // 设置更新时间
        violation.setUpdateTime(LocalDateTime.now());
        
        // 更新处理结果
        boolean result = violationService.updateById(violation);
        return R.success(result);
    }

    /**
     * 删除违规记录
     *
     * @param id 违规记录ID
     * @return 删除结果
     */
    @GetMapping("/delete")
    @ApiOperation("删除违规记录")
    public R<Boolean> delete(@RequestParam Integer id) {
        log.info("删除违规记录: {}", id);
        
        // 使用自定义的删除方法
        try {
            boolean result = violationService.deleteViolationById(id);
            log.info("删除违规记录结果: {}", result);
            return R.success(result);
        } catch (Exception e) {
            log.error("删除违规记录异常: ", e);
            return R.error(false);
        }
    }

    /**
     * 获取违规记录详情
     *
     * @param id 违规记录ID
     * @return 违规记录详情
     */
    @GetMapping("/detail")
    @ApiOperation("获取违规记录详情")
    public R<Violation> detail(@RequestParam Integer id) {
        log.info("获取违规记录详情: {}", id);
        
        Violation violation = violationService.getById(id);
        if (violation == null) {
            return R.error(null);
        }
        return R.success(violation);
    }
}
